#pragma once
#include <ev++.h>
#include <memory>

class GameTimer
{
public:
	GameTimer()
	{}
	void init(struct ev_loop *loop, int second, void(*func_cb)(void *), void * ptr)
	{
		_loop = loop;
		_ev_tstamp = second;
		_func_cb = func_cb;
		_ptr = ptr;
		_ev_timer.data = this;
		ev_timer_init(&_ev_timer, GameTimer::timer_cb,
			_ev_tstamp, _ev_tstamp);
	}
	void start()
	{
		ev_timer_start(_loop, &_ev_timer);
	}
	void stop()
	{
		ev_timer_stop(_loop, &_ev_timer);
	}
	void again()
	{
		ev_timer_again(_loop, &_ev_timer);
	}
	static void timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents)
	{
		GameTimer * self = (GameTimer *)w->data;
		self->_func_cb(self->_ptr);
	}
private:
	void * _ptr;
	ev_tstamp	_ev_tstamp;
	struct ev_loop * _loop;
	ev_timer	_ev_timer;
	void(*_func_cb)(void *);
};